package com.common.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.hutool.core.collection.CollUtil;
import com.common.constant.CommonConstant;
import com.modules.dto.sys.resp.MenuDtoResp;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 拦截器（前端按钮操作权限控制）
 */
public class InterceptorConfig implements HandlerInterceptor{


	/**
	 * 页面渲染
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @throws Exception
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
	}

	/**
	 * 执行后
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @throws Exception
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
	}

	/**
	 * 执行前
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {

		// 请求路径如果是跳转页面，则设置按钮权限
		String url = arg0.getRequestURI();
		if(url.indexOf("views") != -1){
			HttpSession session = arg0.getSession();
			List<MenuDtoResp> list = (List<MenuDtoResp>) session.getAttribute(CommonConstant.POWER_LIST);
			if (CollUtil.isNotEmpty(list)){
				for (MenuDtoResp menuDtoResp : list) {
					if(url.indexOf(menuDtoResp.getUrl()) != -1){
						arg0.setAttribute(menuDtoResp.getOperation(), true);
					}
				}
			}
			System.out.println("--------------------------------[拦截器-路径："+url+"]---正在设置按钮权限-------------------------------");
		}

		//arg0.getRequestDispatcher("/login").forward(arg0, arg1);
		
		return true;
	}

	
}
